<?php
/**
 * $Id: news.class.php 73 2005-12-01 21:32:12Z tito $
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function he($data) {
	return htmlentities($data, ENT_QUOTES, 'UTF-8');
}


class fgsNews {

	/**
	 * get() get a news
	 * @param id id of news
	 */
	function get($id) {
		$query = sprintf('
			SELECT *,
				UNIX_TIMESTAMP(dt_create) as date
			FROM fgs_news
			WHERE id = %d',
			$id
		);

		$sql = fgs::sql();
		$sql->query($query);
		return $sql->getline();
	}

	/** 
	 * getHtml() get a news for HTML renderering
	 * @param id id of a news
	 */
	function getHtml($id) {
		$n = fgsNews::get($id);
		if (!$n)
			return false;
		return he(sprintf('
<html>
<head>
<title>%s</title>
<style type="text/css">
h1 { font-size: 18px; border-bottom: 1px solid #aaaaaa; padding: 2px; margin: 5px; }
.date { color: #aaaaaa; margin: 0px 6px 5px 6px; }
.data { padding: 10px; }
</style>
</head>
<body>
<h1>%s</h1>
<div class="date">Date: %s</div>
<div class="data">%s</div>
</body>
</html>',
			he($n['title']),
			he($n['title']),
			he(fgsUtils::formatDate($n['date'])),
			nl2br(he($n['text']))
		));
	}

	/**
	 * getLast() get last news 
	 * @param lang filter on language {fr, en, ...}
	 * @param limit return n rows
	 * @param cat filter on category
	 */
	function getLast($lang = null, $limit = 5, $cat = null, $isonline = true) {

		// vars
		$news = array();
		$where = array();

		// construct where
		if (!empty($lang))
			$where[] = sprintf('lang = "%s"', fgsSql::escapeStr($lang));
		if (!empty($cat))
			$where[] = sprintf('category = "%s"', fgsSql::escapeStr($cat));
		if ($isonline)
			$where[] = 'is_online';
	
		// construct query
		$query = sprintf('
			SELECT *,
				UNIX_TIMESTAMP(dt_create) as date
			FROM fgs_news
			%s
			ORDER BY dt_create DESC
			%s',
			count($where) ? 'WHERE '.implode(' AND ', $where) : '',
			is_null($limit) ? '' : sprintf('LIMIT %d', $limit)
		);

		// execute query
		$sql = fgs::sql();
		$sql->query($query);

		// handle data
		$news = false;
		if ($sql->nbRows()) {
			$news = $sql->fetchAll();
			foreach ($news as $key => $value) {
				$news[$key]['title'] = $value['title'];
				$news[$key]['text'] = nl2br($value['text']);
			}
		}
		
		return $news;
	}

	/**
	 * getCategories() list of news categories
	 */
	function getCategories() {
		$query = 'SELECT * FROM fgs_news_categories';

		$sql = fgs::sql();
		$sql->query($query);
		
		return $sql->fetchAll();
	}

	/**
	 * getLogo() get logo from hash
	 * @param hash logo hash
	 */
	function getLogo($hash) {
		$query = sprintf('
			SELECT logo_data
			FROM fgs_news_categories
			WHERE logo_hash = "%s"',
			fgsSql::escapeStr($hash)
		);

		$sql = fgs::sql();
		$sql->query($query);

		return $sql->getline();
	}

	/**
	 * add() add a news
	 */
	function add($title, $subtitle, $category, $text, $lang, $idAuthor, $isOnline) {
		$query = sprintf('
			INSERT INTO fgs_news
			SET 
				title = "%s",
				subtitle = "%s",
				category = %d,
				text = "%s",
				lang = "%s",
				id_author = %d,
				dt_create = NOW(),
				is_online = %d',
			fgsSql::escapeStr($title),
			fgsSql::escapeStr($subtitle),
			$category,
			fgsSql::escapeStr($text),
			fgsSql::escapeStr($lang),
			$idAuthor,
			$isOnline ? 1 : 0
		);

		$sql = fgs::sql();
		$res = $sql->query($query);

		return $res ? true : false;
	}

	/**
	 * update() update a news
	 */
	function update($id, $title, $subtitle, $category, $text, $lang, $idAuthor, $isOnline) {
		$query = sprintf('
			UPDATE fgs_news
			SET 
				title = "%s",
				subtitle = "%s",
				category = %d,
				text = "%s",
				lang = "%s",
				id_author = %d,
				is_online = %d
			WHERE id = %d',
			fgsSql::escapeStr($title),
			fgsSql::escapeStr($subtitle),
			$category,
			fgsSql::escapeStr($text),
			fgsSql::escapeStr($lang),
			$idAuthor,
			$isOnline ? 1 : 0,
			$id
		);

		$sql = fgs::sql();
		$res = $sql->query($query);

		return $res ? true : false;
	}

	/**
	 * makeOnline() make a news online
	 */
	function makeOnline($id, $online) {
		$query = sprintf('
			UPDATE fgs_news
			SET 
				is_online = %d
			WHERE id = %d',
			$online ? 1 : 0,
			$id
		);

		$sql = fgs::sql();
		$res = $sql->query($query);

		return $res ? true : false;
	}

}

?>
